package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.*;
import com.lczc.soa.mapper.DoorMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DoorServiceImpl implements DoorService{
    @Autowired
    private DoorMapper doorMapper;

   @Override

    public int saveDoor(Door door) {
        if(StringUtils.isNotBlank(door.getDoorId())){
            return doorMapper.updateByPrimaryKeySelective(door);
        }else {
            return doorMapper.insert(door);
        }
    }

    @Override
    public SearchDoorResponse findAllDoor(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //List<Door> doors = doorMapper.selectByExample(new DoorExample());
        List<Door> doors = doorMapper.selectAll();
        return new SearchDoorResponse(((Page)doors).getTotal(),doors);
    }

    @Override
    public Door findDoorByPrimaryKey(String id) {
        return doorMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteDoor(String id) {
        return doorMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SearchDoorResponse findDoorLikeDoorNumber(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Door> doors = doorMapper.selectByLikeDoorNumber(keywords);
        return new SearchDoorResponse(((Page)doors).getTotal(),doors);
    }

    @Override
    public List<Door> listDoor() { return doorMapper.selectAll(); }

    @Override
    public List<Door> findDoorByStreet(String id) {
        return doorMapper.selectByStreet(id);
    }
}
